package day3;



import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * ClassName：Test2
 *
 * @Author：小鱼
 * @Create：2025-08-13 14:25
 */
public class Test2 {
    public static void main(String[] args) {
        //比较器排序实现
        Comparator<Student> comparator = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int num;
                //叫tom的学⽣不管考多少分都位于班级的第⼀位
                if("tom".equals(o1.getName())){
                    return -1;
                }
                if("tom".equals(o2.getName())){
                    return 1;
                }
                //name进行降序排序
                num = -(o1.getName().compareTo(o2.getName()));
                if(num==0){
                    //age升序排序
                    num = o1.getAge()-o2.getAge();
                }
                if(num==0){
                    //score降序排序
                    num = -Double.compare(o1.getScore(), o2.getScore());;
                }
                return num;
            }
        };

        //创建学生对象
        Student s1 = new Student("zs",20,85.2);
        Student s2 = new Student("tom",18,74);
        Student s3 = new Student("ls",20,90);
        Student s4 = new Student("ww",19,70);
        Student s5 = new Student("zs",19,70);
        Student s6 = new Student("zs",20,70);

        //自然排序实现
        //创建集合，存放Student
        Set<Student>set = new TreeSet();
        set.add(s1);
        set.add(s2);
        set.add(s3);
        set.add(s4);
        set.add(s5);
        set.add(s6);
        System.out.println("自然排序实现结果为: "+set);

       // 比较器排序实现
        Set<Student>set2 = new TreeSet(comparator);
        set2.add(s1);
        set2.add(s2);
        set2.add(s3);
        set2.add(s4);
        set2.add(s5);
        set2.add(s6);
        System.out.println("比较器排序实现结果为: "+set2);
    }
}
